package com.njupt.Tree;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: wujiaming
 * @CreateTime: 2024/11/19 10:05
 * @Description: 二叉树的后序遍历
 * @Version: 1.0
 */


public class PostorderTraversal {


    public void postorder(TreeNode root,List<Integer> result){
        if(root == null) return;

        postorder(root.left,result);

        postorder(root.right,result);
        result.add(root.val);
    }

    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> results = new ArrayList<>();
        postorder(root,results);
        return results;
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        TreeNode node1 = new TreeNode(2);
        TreeNode node2 = new TreeNode(3);
        root.right = node1;
        node1.left = node2;
        PostorderTraversal test = new PostorderTraversal();
        List<Integer> list = test.postorderTraversal(root);
        System.out.println(list.toString());
    }
}
